//-----------------------------------------------------------------------
// <copyright file="DoiTacDAOBase.cs" company="Pyramid Consulting">
//     Copyright (c) Pyramid Consulting. All rights reserved.
// </copyright>
//-----------------------------------------------------------------------

using System;
using System.Data;
using System.Data.Common;
using System.Collections;
using Microsoft.Practices.EnterpriseLibrary.Data;
using APPro.Entities;

using Pyco;
using Pyco.Diagnostics;
using Pyco.Data;

namespace APPro.Data
{    
    public abstract class DoiTacDAOBase
    {
        #region Common methods
        public virtual DoiTac CreateDoiTacFromReader(IDataReader reader)
        {
            DoiTac item = new DoiTac();
            try
            {
                if (!reader.IsDBNull(reader.GetOrdinal("MaDoiTac"))) item.MaDoiTac = (int)reader["MaDoiTac"];
                if (!reader.IsDBNull(reader.GetOrdinal("TenDoiTac"))) item.TenDoiTac = (string)reader["TenDoiTac"];
                if (!reader.IsDBNull(reader.GetOrdinal("NoiDung"))) item.NoiDung = (string)reader["NoiDung"];
                if (!reader.IsDBNull(reader.GetOrdinal("ChiTiet"))) item.ChiTiet = (string)reader["ChiTiet"];
                if (!reader.IsDBNull(reader.GetOrdinal("DanhMucDoiTac"))) item.DanhMucDoiTac = (int)reader["DanhMucDoiTac"];
            }
            catch (Exception ex)
            {
                // log this exception
                Logger.Error(ex);
                // wrap it and rethrow
                throw new DataAccessException(SRBase.DataAccessCreateDoiTacFromReaderException, ex);
            }
            return item;
        }
        #endregion
		
        #region CreateDoiTac methods
            
        public virtual void CreateDoiTac(DoiTac doiTac)
        {
            try
            {
                Database database = DatabaseFactory.CreateDatabase();
                DbCommand dbCommand = database.GetStoredProcCommand("spDoiTacInsert");
                
				database.AddInParameter(dbCommand, "@MaDoiTac", DbType.Int32, doiTac.MaDoiTac);
				database.AddInParameter(dbCommand, "@TenDoiTac", DbType.String, doiTac.TenDoiTac);
				database.AddInParameter(dbCommand, "@NoiDung", DbType.String, doiTac.NoiDung);
				database.AddInParameter(dbCommand, "@ChiTiet", DbType.String, doiTac.ChiTiet);
				database.AddInParameter(dbCommand, "@DanhMucDoiTac", DbType.Int32, doiTac.DanhMucDoiTac);
				
                DataAccessHelper.ExecuteNonQuery(database, dbCommand);
                
            }
            catch (Exception ex)
            {
                // log this exception
                Logger.Error(ex);
                // wrap it and rethrow
                throw new DataAccessException(SRBase.DataAccessCreateDoiTacException, ex);
            }
        }

        #endregion
		
        #region UpdateDoiTac methods
        
        public virtual void UpdateDoiTac(DoiTac doiTac)
        {
            try
            {
                Database database = DatabaseFactory.CreateDatabase();
                DbCommand dbCommand = database.GetStoredProcCommand("spDoiTacUpdate");            
                
                database.AddInParameter(dbCommand, "@MaDoiTac", DbType.Int32, doiTac.MaDoiTac);
                database.AddInParameter(dbCommand, "@TenDoiTac", DbType.String, doiTac.TenDoiTac);
                database.AddInParameter(dbCommand, "@NoiDung", DbType.String, doiTac.NoiDung);
                database.AddInParameter(dbCommand, "@ChiTiet", DbType.String, doiTac.ChiTiet);
                database.AddInParameter(dbCommand, "@DanhMucDoiTac", DbType.Int32, doiTac.DanhMucDoiTac);
                
                DataAccessHelper.ExecuteNonQuery(database, dbCommand);
            }
            catch (Exception ex)
            {
                // log this exception
                Logger.Error(ex);
                // wrap it and rethrow
                throw new DataAccessException(SRBase.DataAccessUpdateDoiTacException, ex);
            }
        }
        
        #endregion
		
        #region DeleteDoiTac methods
        public virtual void DeleteDoiTac(int maDoiTac)
        {
            try
            {
                Database database = DatabaseFactory.CreateDatabase();
                DbCommand dbCommand = database.GetStoredProcCommand("spDoiTacDelete");
                
                database.AddInParameter(dbCommand, "@MaDoiTac", DbType.Int32, maDoiTac);
                
                DataAccessHelper.ExecuteNonQuery(database, dbCommand);
            }
            catch (Exception ex)
            {
                // log this exception
                Logger.Error(ex);
                // wrap it and rethrow
                throw new DataAccessException(SRBase.DataAccessDeleteDoiTacException, ex);
            }
        }

        #endregion
		
        #region GetDoiTac methods
        
        public virtual DoiTac GetDoiTac(int maDoiTac)
        {            
            try
            {
                Database database = DatabaseFactory.CreateDatabase();
                DbCommand dbCommand = database.GetStoredProcCommand("spDoiTacGet");
                
                database.AddInParameter(dbCommand, "@MaDoiTac", DbType.Int32, maDoiTac);
                
                DoiTac doiTac =  null;
                using (IDataReader reader = DataAccessHelper.ExecuteReader(database, dbCommand))
                {
                    if (reader.Read())
                    {
                        doiTac = CreateDoiTacFromReader(reader);
                        reader.Close();
                    }
                }
                return doiTac;
            }
            catch (Exception ex)
            {
                // log this exception
                Logger.Error(ex);
                // wrap it and rethrow
                throw new DataAccessException(SRBase.DataAccessGetDoiTacException, ex);
            }
        }
        
        #endregion
		
        #region GetDoiTacList methods
        public virtual DoiTacCollection GetDoiTacList(DoiTacColumns orderBy, OrderDirection orderDirection, int page, int pageSize, out int totalRecords)
        {            
            try
            {
                Database database = DatabaseFactory.CreateDatabase();
                DbCommand dbCommand = database.GetStoredProcCommand("spDoiTacGetList");
                
                database.AddInParameter(dbCommand, "@OrderBy", DbType.AnsiString, orderBy.ToString());
                database.AddInParameter(dbCommand, "@OrderDirection", DbType.AnsiString, orderDirection.ToString());
                database.AddInParameter(dbCommand, "@Page", DbType.Int32, page);
                database.AddInParameter(dbCommand, "@PageSize", DbType.Int32, pageSize);
                database.AddOutParameter(dbCommand, "@TotalRecords", DbType.Int32, 4);
                
                DoiTacCollection doiTacCollection = new DoiTacCollection();
                using (IDataReader reader = DataAccessHelper.ExecuteReader(database, dbCommand))
                {
                    while (reader.Read())
                    {
                        DoiTac doiTac = CreateDoiTacFromReader(reader);
                        doiTacCollection.Add(doiTac);
                    }
                    reader.Close();
                }
                totalRecords = (int)database.GetParameterValue(dbCommand, "@TotalRecords");
                return doiTacCollection;
            }
            catch (Exception ex)
            {
                // log this exception
                Logger.Error(ex);
                // wrap it and rethrow
                throw new DataAccessException(SRBase.DataAccessGetDoiTacListException, ex);
            }
        }
        
        public virtual DoiTacCollection GetDoiTacList(DoiTacColumns orderBy, OrderDirection orderDirection)
        {            
            int totalRecords = 0;
            return GetDoiTacList(orderBy, orderDirection, 0, 0, out totalRecords);
        }
        
        #endregion
		
    }
}